Skip to main content

Rapha Terraform Styleguide

Overview

This document serves as the complete definition of Rapha’s coding standards for source code in the Terraform programming language. A Terraform source file is described as being in Rapha Style if and only if it adheres to the rules herein.

Beacuse Terraform allows our infrastructure to be described as code, we must adhere to a style guide to ensure readable and high quality code.

Naming Conventions

File Names

Create a separate resource file for each type of AWS resource. Similar resources should be defined in the same file and named accordingly.

ami.tf
autoscaling_group.tf
cloudwatch.tf
iam.tf
launch_configuration.tf
providers.tf
s3.tf
security_groups.tf
sns.tf
sqs.tf
user_data.sh
variables.tf
```

## Parameter, Meta-parameter and Variable Naming

**Only use an underscore (\_)** when naming Terraform resources like TYPE/NAME parameters and variables.

```terraform
resource "aws_security_group" "security_group" {
...
}

Resource Naming

Only use a hyphen (-) when naming the component being created.

resource "aws_security_group" "security_group" {
name = "${var.resource_name}-security-group"
...
}

Naming of entities

When naming resources we must always follow the next format: Ecosystem_entity_resource

  • Ecosystem: represents where the resource belongs: NAV, Platform, commercecloud
  • Entity: product, price ...
  • Resource: is what we are naming, in this case we will always go from the action to the final resource to name it, an example would be deduplication_sqs_ssm_parameter we indicate first what is or where we are and finally the resource
# Platform
resource "aws_cloudwatch_event_target" "platform_product_deduplication_cloudwatch_event_target" {
target_id = "platform-${var.environment}-product-deduplication-target"
rule = aws_cloudwatch_event_rule.nav_create_product_cloudwatch_event_rule.name
event_bus_name = aws_cloudwatch_event_bus.product_event_bus.name
arn = data.aws_ssm_parameter.platform_product_deduplication_sqs_ssm_parameter.value
}

Spacing

Use 2 spaces when defining resources except when defining inline policies or other inline resources. Spacing and formatting can be enforced by running terraform fmt*

resource "aws_iam_role" "iam_role" {
name = "${var.resource_name}-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}

Resource Block Alignment

Parameter definitions in a resource block should be aligned.

provider "aws" {
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
region = "eu-west-1"
}

Comments

When commenting use a hash "#" and a space in front of the comment.

# ADD YOUR COMMENTS HERE
...

Organising Variables

The variables.tf file should be broken down into three sections with each section arranged alphabetically. Starting at the top of the file:

  1. Variables that have no defaults defined
  2. Variables that contain defaults
  3. All locals block
variable "image_tag" {}

variable "desired_count" {
default = "2"
}

locals {
domain_name = "${data.terraform_remote_state.account.domain_name}"
}

Resources

Terraform Best Practises